<?php

class PhotoModel extends Model {
	public $tableName = "photo";
    public static $UPLOAD_DIR = "public/img/";

    public static $ACCESS_EVERY = 4;
    public static $ACCESS_LOGIN = 3;
    public static $ACCESS_FOLLOWER = 2;
    public static $ACCESS_PRIVATE = 1;

    public static $COMMENT_ALLOWED = 2;
    public static $COMMENT_NOT_ALLOWED = 1;
	
	protected $belongs_to = array('bm_auth');
    protected $has_many = array('hm_wall', 'hm_tag');
	
	public function __construct($query = null) {
		parent::__construct($query);
	}

    public function tag($user) {
        if (!$this->isTagged($user))
            Model::query("INSERT INTO `tag`(`photo_id` , `user_id`) VALUES(".$this->id." , ".$user->id.");");
    }

    public function untag($user) {
        if ($this->isTagged($user))
            Model::query("DELETE FROM `tag` WHERE `photo_id`= ".$this->id." AND `user_id` = ".$user->id.";");
    }

    public function clearTag() {
        Model::query("DELETE FROM `tag` WHERE `photo_id`= ".$this->id.";");
    }

    public function getUrl() {
        return $GLOBALS['base_path'] . PhotoModel::$UPLOAD_DIR . $this->name;
    }

    public function isTagged($user) {
        $rows = Model::getRowsFromQuery("SELECT * FROM `tag` WHERE `photo_id` = ".$this->id." AND `user_id` = " . $user->id . ";");
        return count($rows) > 0;
    }
	
}

?>